package biz.inspeed.test.mina.android;

import org.apache.mina.core.buffer.AbstractIoBuffer;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class MinaIoHandler implements IoHandler
{

	@Override
	public void sessionCreated ( IoSession session ) throws Exception
	{
		System.out.println ( "创建了一个连接"  + session.getRemoteAddress ().toString ());
		
	}

	@Override
	public void sessionOpened ( IoSession session ) throws Exception
	{
		System.out.println ( "打开了一个连接"  + session.getRemoteAddress ().toString ());
	}

	@Override
	public void sessionClosed ( IoSession session ) throws Exception
	{
		System.out.println ( "关闭了一个连接"  + session.getRemoteAddress ().toString ());
	}

	@Override
	public void sessionIdle ( IoSession session , IdleStatus status ) throws Exception
	{
		System.out.println ( "现在是空闲状态"  );
	}

	@Override
	public void exceptionCaught ( IoSession session , Throwable cause ) throws Exception
	{
		System.out.println ( "遇到了一个异常" + cause.getCause ().toString () );
	}

	@Override
	public void messageReceived ( IoSession session , Object message ) throws Exception
	{
		System.out.println ( "收到一个数据"  + message.toString ());
		AbstractIoBuffer aib = (AbstractIoBuffer) message;
		IoBuffer ib = aib.shrink();
		byte[] array = ib.array();
		System.out.println(new String(array));
		System.out.println(message.getClass().toString());
//		session.write ( "Darling,This is a message from Server!" );
	}

	@Override
	public void messageSent ( IoSession session , Object message ) throws Exception
	{
		System.out.println ( "发送信息完成" );
	}


}
